package com.example.demo.controller;

import com.example.demo.service.BookService;
import com.example.demo.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class BorrowController {
    @Autowired
    BookService bookService;
    @Autowired
    UserService userService;
    private static final Logger logger = LoggerFactory.getLogger(BorrowController.class);
    @Operation(summary = "首页")
    @GetMapping({"/index", "/"})
    public String borrow() {
        return "index";
    }
    @GetMapping("/add-borrow")
    public String addBorrow(Model model) {
        model.addAttribute("book_list", bookService.getActiveBookList());
        return "add-borrow";
    }
    @PostMapping("/add-borrow")
    public String addBorrow(int student, int book) {
        bookService.addBorrow(student, book);
        return "redirect:/index";
    }
    @GetMapping("/return-book")
    public String returnBook(Integer id){
        bookService.returnBorrow(id);
        return "redirect:/index";
    }
}
